﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ApproveFlowEngine.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace ApproveFlowEngine.EntityFrameworkCoreSplitTable.EFCore.Core
{
    public class SplitModelCustomizer : ModelCustomizer
    {
        public SplitModelCustomizer(ModelCustomizerDependencies dependencies) : base(dependencies)
        {
        }

        public override void Customize(ModelBuilder modelBuilder, DbContext context)
        {
            base.Customize(modelBuilder, context);
            if (context is SplitDbContext)
            {
                foreach (var item in TableTailHelper.GetTail())
                {
                    var entity = modelBuilder.Model.FindEntityType(item.Key);
                    entity?.SetTableName($"{entity.GetTableName()}{item.Value}");
                }
            }
        }
    }
}